﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class FrontEnd_Control_Member_Popup
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            BindToGrid()
        End If
    End Sub
    Private Sub BindToGrid(Optional ByVal strFilter As String = "", Optional ByVal iPage As Integer = 1)
        Using Data As New VSEntities
            Dim arrSearch() As String = {strFilter, iPage}
            ViewState("search") = arrSearch
            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            'lấy danh sách mã và tên lĩnh vực kinh doanh
            Dim curId As Integer = IIf(Request("Id") = "", 0, Request("Id"))
            If strFilter = "" And curId = 0 Then
                Dim ListNganhNghe1 = (From q In Data.DanhMucNganhNghes _
                                     Where q.ParentId = 0 Order By q.Name Descending Select q.Code, q.Name).ToList()
                Dim strKey_Name() As String = {"Code", "Name"}
                With grdShow
                    .DataKeyNames = strKey_Name
                    .DataSource = ListNganhNghe1
                    .DataBind()
                End With
            ElseIf curId <> 0 And strFilter = "" Then
                Dim ListNganhNghe2 = (From q In Data.DanhMucNganhNghes _
                                     Where q.ParentId = curId Order By q.Name Descending Select q.Code, q.Name).ToList()
                Dim strKey_Name() As String = {"Code", "Name"}
                With grdShow
                    .DataKeyNames = strKey_Name
                    .DataSource = ListNganhNghe2
                    .DataBind()
                End With
            Else
                Dim ListNganhNghe3 = (From q In Data.DanhMucNganhNghes _
                                     Where (q.ParentId = curId Or q.Name.Contains(strFilter) Or q.Code.Contains(strFilter)) Order By q.Name Descending Select q.Code, q.Name).ToList()
                Dim strKey_Name() As String = {"Code", "Name"}
                With grdShow
                    .DataKeyNames = strKey_Name
                    .DataSource = ListNganhNghe3
                    .DataBind()
                End With
            End If



            lblTotal.Text = "Hiển thị " + (((iPage - 1) * intPag_Size) + 1).ToString + " đến " + (((iPage - 1) * intPag_Size) + grdShow.Rows.Count).ToString _
                            + " trong tổng số " + hidCount.Value.ToString + " bản ghi."
        End Using
    End Sub
#End Region
#Region "Event for control"
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region

#Region "Search"
    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        BindToGrid(txtFilter.Text.Trim())
    End Sub
#End Region

    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim chkItem As CheckBox = CType(e.Row.FindControl("chkItem"), CheckBox)
            Dim hplCode As HyperLink = CType(e.Row.FindControl("hplCode"), HyperLink)
            Dim hplName As HyperLink = CType(e.Row.FindControl("hplName"), HyperLink)
            Using _data As New VSEntities
                Dim code = grdShow.DataKeys(e.Row.RowIndex)("Code").ToString
                Dim checkParent = (From q In _data.DanhMucNganhNghes Where q.Code = code Select q.Id, q.ParentId).FirstOrDefault
                If Not checkParent Is Nothing Then
                    hplCode.Text = grdShow.DataKeys(e.Row.RowIndex)("Code").ToString
                    hplCode.NavigateUrl = MainDomain & "/FrontEnd/Page/Member/Popup.aspx?item=0&keepThis=true&Id=" & checkParent.Id

                    hplName.Text = grdShow.DataKeys(e.Row.RowIndex)("Name").ToString
                    hplName.NavigateUrl = MainDomain & "/FrontEnd/Page/Member/Popup.aspx?item=0&keepThis=true&Id=" & checkParent.Id

                End If
            End Using



            If Not Request.QueryString("item") Is Nothing Then
                chkItem.Attributes.Add("onclick", "javacript:setProductParentValues('" & grdShow.DataKeys(e.Row.RowIndex)("Code").ToString & "','" & grdShow.DataKeys(e.Row.RowIndex)("Name").ToString & "','" & Request.QueryString("item") & "')")
            Else
                chkItem.Attributes.Add("onclick", "javacript:setSubjectParentValues('" & grdShow.DataKeys(e.Row.RowIndex)("Code").ToString & "')")
            End If
        End If
    End Sub
    Protected Sub lbtnUpOneLevel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnUpOneLevel.Click
        Dim Id = Request("Id")
        If Id = "" Or Id = "0" Then
            Response.Redirect(MainDomain & "/FrontEnd/Page/Member/Popup.aspx?item=0&keepThis=true")
        Else
            Using data As New VSEntities
                Dim p = (From q In data.DanhMucNganhNghes Where q.Id = Id Select q.ParentId).FirstOrDefault
                Response.Redirect(MainDomain & "/FrontEnd/Page/Member/Popup.aspx?item=0&keepThis=true&Id=" & p)
            End Using
        End If
    End Sub
End Class
